// Copyright (c) 2022 Presto Labs Pte. Ltd.
// Author: fengyang

#pragma once

#include "coin2/exchange/base/feed_util/book.h"

namespace coin2::exchange::base::feed::replay {

// only support snapshot as the most of cases are light_bbo and move2bp
enum class BookEventType {
  Unknow,
  Snapshot,
};

struct BookEvent {
  BookEventType type;
  int64_t timestamp;
  std::vector<PriceQty> asks;
  std::vector<PriceQty> bids;
};

class BookEventBuilder {
 public:
  BookEventBuilder(int depth)
  : depth_(depth) {
  }

  BookEvent ToEvent(const IBook& book) {
    BookEvent event;
    event.type = BookEventType::Snapshot;
    event.timestamp = book.Timestamp();

    int level = 1;
    auto ask_iter = book.AskBegin();
    for ( ; ask_iter != book.AskEnd() && level <= depth_; ) {
      event.asks.emplace_back(PriceQty(ask_iter->first, ask_iter->second));
      ++level;
      ++ask_iter;
    }

    level = 1;
    auto bid_iter = book.BidBegin();
    for ( ; bid_iter != book.BidEnd() && level <= depth_; ) {
      event.bids.emplace_back(PriceQty(bid_iter->first, bid_iter->second));
      ++level;
      ++bid_iter;
    }

    return event;
  };

 private:
  int depth_;
};

}  // namespace coin2::exchange::base::feed::replay
